package com.th.providerprepaid_refill.controller;

import com.lww.springcloudapi.pojo.TabHallUserpayment;
import com.lww.springcloudapi.result.LayuiEntity;
import com.lww.springcloudapi.result.Result;
import com.lww.springcloudapi.result.ResultCodeEnum;
import com.th.providerprepaid_refill.entity.vo.PrepaidRefillData;
import com.th.providerprepaid_refill.service.PrepaidRefillService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import redis.clients.jedis.Jedis;

import java.util.List;

/**
 * 缴费及缴费记录查询的Controller控制器
 */
@RestController
@RequestMapping("/propaidRefill")
@Api(tags = "缴费及相关记录查询模块")
@CrossOrigin
public class PropaidRefillController {

    @Autowired
    PrepaidRefillService service;   //缴费充值的service

    /**
     * 缴费充值的接口
     *
     * @param data 传入用户输入的金额的数据
     * @return 统一结果类
     */
    @CrossOrigin //解决跨域问题
    @PostMapping("/recharge")
    @ApiOperation(value = "缴费充值")
    @ResponseBody
    public LayuiEntity getProduct( PrepaidRefillData data) {
        System.out.println(data);
        LayuiEntity layuiEntity = new LayuiEntity();
        try {
            Integer result = service.recharge(data);
            //服务访问成功的结果 ==返回：查询成功信息
            if (result != null && !result.equals("")) {
                layuiEntity.setMsg(ResultCodeEnum.SUCCESS.getMessage());
                layuiEntity.setCode(ResultCodeEnum.SUCCESS.getCode()+"");
            } else {  //未查询到数据 ==返回：数据异常信息
                layuiEntity.setMsg(ResultCodeEnum.DATA_ERROR.getMessage());
                layuiEntity.setCode(ResultCodeEnum.DATA_ERROR.getCode()+"");
            }
        } catch (Exception e) {
            e.printStackTrace();    //错误信息打印
            //服务访问出错的结果 ==返回：服务异常信息
            layuiEntity.setMsg(ResultCodeEnum.SERVICE_ERROR.getMessage());
            layuiEntity.setCode(ResultCodeEnum.SERVICE_ERROR.getCode()+"");
        }
        return layuiEntity;
    }

    /**
     * 缴费记录查询的接口
     *
     *
     * @return 统一结果类
     */
    @CrossOrigin //解决跨域问题
    @GetMapping("/selectRefillRecording")
    @ApiOperation(value = "查询缴费记录")
    public LayuiEntity<List<TabHallUserpayment>> selectRefillRecording(Integer page,Integer limit) {
        LayuiEntity layuiEntity = new LayuiEntity();
        Jedis jedis = new Jedis("localhost");
        String pnumber = jedis.get("user");
        try {
            List<TabHallUserpayment> resultList = service.selectRefillRecording(pnumber,page,limit);
            Integer count = service.selectCount(pnumber);
            //服务访问成功的结果 ==返回：查询成功信息
            if (resultList != null && !resultList.equals("")&& count!=null && !count.equals("")) {
                layuiEntity.setData(resultList);
                layuiEntity.setCount(count);
                layuiEntity.setMsg(ResultCodeEnum.SUCCESS.getMessage());
                layuiEntity.setCode(ResultCodeEnum.SUCCESS.getCode()+"");
            } else {  //未查询到数据 ==返回：数据异常信息
                layuiEntity.setMsg(ResultCodeEnum.DATA_ERROR.getMessage());
                layuiEntity.setCode(ResultCodeEnum.DATA_ERROR.getCode()+"");
            }

        } catch (Exception e) {
            e.printStackTrace();    //错误信息打印
            //服务访问出错的结果 ==返回：服务异常信息
            layuiEntity.setMsg(ResultCodeEnum.SERVICE_ERROR.getMessage());
            layuiEntity.setCode(ResultCodeEnum.SERVICE_ERROR.getCode()+"");
        }
        return layuiEntity;
    }


}
